<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Team. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/docs
 */

class Component_Appstore_Helpers_Product extends appRain_Base_Objects
{
	public function getRelatedProduct($cid)
	{
		$data = App::InformationSet('product')->findAll(null,Array(Array("category"=>"%{$cid}%",'cnd'=>'LIKE')));
		return $data['data'];
	}

	public function getRelatedHoodie($cid)
	{
		$data = App::InformationSet('hoodie')->findAll(null,Array(Array("bear-category"=>"%{$cid}%",'cnd'=>'LIKE'),'AND',array('status'=>'Public')));
		return $data['data'];
	}

	public function getRelatedCard($cid)
	{
		$data = App::InformationSet('card')->findAll(null,Array(Array("bear-category"=>"%{$cid}%",'cnd'=>'LIKE'),'AND',array('status'=>'Public')));
		return $data['data'];
	}

	public function personalisedData($isHTML=true)
	{
		$data = App::Model('Item')->findByid($this->getItemId());

		if(!isset($data['productdata'])) return "";

		$productdata = explode('|#|',unserialize($data['productdata']));

		if(empty($productdata)) return "";

		$arr = array();
		foreach($productdata as $val)
		{
			$dt = explode('|@|',$val);
			$arr[$dt[0]] = $dt[1];
		}

		$orderData = $arr;
		if(isset($orderData['special_instruction']))unset($orderData['special_instruction']);

		$bear = App::InformationSet()->findById($arr['bear']);
		$orderData['bear'] = $bear['title'];
		///pre($bear);

		$hoodie = App::InformationSet()->findById($arr['hoodie']);
		$orderData['hoodie'] = $hoodie['title'];

		$card = App::InformationSet()->findById($arr['card']);
		$orderData['card'] = $card['title'];

		$category = App::CategorySet()->findById($arr['bear_type']);

		$orderData['bear_type'] = $category['title'];

		if($isHTML)
		{
			$html = '<table border="0" cellpadding="3" cellspacing="0">';
			foreach($orderData as $key=>$val)
			{
				$html .= "<tr>
							<th align=\"left\" style=\"background-color:#D9E5EE\">{$key}</th><td style=\"border:1px solid #D9E5EE\">{$val}</td></tr>";
			}
			$html .= '</table>';

			return $html;
		}
		else  return $orderData;
	}
}